<!--头部公共部分-->
{include file="common/header" /}

<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>首页</a>
        <a>设置</a>
        <a>
          <cite>日期管理</cite></a>
      </span>
    <a class="layui-btn layui-btn-xs layui-btn-small" style="float:right;margin-top: 10px;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh"></i> </a>
</div>
<div id="wodekehu" class="x-body" class="clear:both;">
    <div class="layui-row" style="padding-left:20px;border-left:1px solid #e5e5e5;border-right:1px solid #e5e5e5;">
        <div class="layui-form layui-col-md12 x-so layui-form-pane">
            <div class="layui-input-inline">
                <input type="text" name="date_name" id="date_search" placeholder="请输入日期" autocomplete="off" class="layui-input">
                <input type="hidden" id="company" name="company" value="">
            </div>
            <div class="layui-input-inline">
                <button class="layui-btn" id="search">
                    <i class="layui-icon layui-icon-search"></i>筛选</button>
                <button class="layui-btn layui-btn-danger" id="clear">
                    <i class="layui-icon"></i>重置</button>
                <!--
                <button class="layui-btn" id="table-synchron">
                    <i class="layui-icon layui-icon-refresh-3"></i>重新同步</button>
                    -->
            </div>
        </div>
    </div>
    <fieldset class="layui-elem-field" style="border-width: 3px; border-color: #00b7ee;">
        <legend>节假日 - 设定</legend>
        <div class="layui-field-box">
    <div class="layui-row" style="margin-left: 10px; margin-right: 10px" >
        <div class="layui-col-md3">
            <div id="datechoice"></div>
        </div>
        <div class="layui-col-md9" style="margin-top: -10px">
            <table class="layui-hide" id="LAY_table_user" lay-filter="table"></table>
            <script type="text/html" id="barTable">
                <a class="layui-btn layui-btn-xs layui-btn-danger " lay-event="del">删除</a>
            </script>
        </div>
    </div>
        </div>
    </fieldset>
</div>
<!--底部公共部分-->
{include file="common/footer" /}

<script>
    function company(v) {
        console.log(v)
        console.log(v.field.company_id)
        $("#company").val(v.field.company_id);
    }
    var last_value = getNowFormatDate();  //上个日期
    var last_month = "";  //上个月
    var last_year = "";  //上个年
    layui.use(['form','laydate','element', 'table', 'layer', 'laytpl'], function () {
        var $ = layui.$,
            form = layui.form,
            table = layui.table,
            element = layui.element,
            laydate = layui.laydate,
            layer = layui.layer;

        var loading = layer.load();
        let type = "{$type}";
        //同步
        $('#table-synchron').click(function () {
            var isLoad;
            var field = {};
            field.class_subject_id = 19;  //课程表

            $.ajax({
                url: '/admin/Holiday/synchrondata',
                method: "post",
                data: field,
                beforeSend: function () {
                    isLoad = layer.load(0, {
                        shade: false
                    }); //0代表加载的风格，支持0-2
                },
                success: function (result) {
                    layer.msg(result.msg);
                    if (result.code === 1) {

                        setTimeout(function () {
                            location.reload();
                        }, 1000);


                    }
                },
                complete: function () {
                    layer.close(isLoad)
                },
                error: function (error) {
                    console.log(error)
                }
            });
            form.render(); //重新渲染
            return false;
        });

        var date_value = laydate.render({
            elem: '#datechoice'
            ,position: 'static'
            ,trigger: 'click'
            ,btns: ['clear', 'confirm']
            ,calendar: true
            ,change: function(value, date, endDate){
                var arr_date = value.split("-");
                if(arr_date[0] != last_year || arr_date[1] != last_month){
                    last_value = value;
                }
                last_year = arr_date[0];
                last_month = arr_date[1];
                /*
                console.log("cehnben111"+value); //得到日期生成的值，如：2017-08-18
                console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
                console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
                */
            }
            ,done: function(value, date, endDate){
                if(last_value == value){  //确定按钮
                    /*
                    console.log(value); //得到日期生成的值，如：2017-08-18
                    console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
                    console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
                    */
                    //异步提交
                    if(type == 1){
                        layer.open({
                            type: 2,
                            skin: 'layui-layer-lan',
                            closeBtn: 2,
                            title: '选择集团',
                            area: ['50%', '60%'],
                            shadeClose: false,
                            content: "{:url('Holiday/company')}?date="+last_value,
                            end:function () {

                                if($("#company").val().length <= 0){
                                    return;
                                }

                                layer.prompt({
                                    formType: 2,
                                    value: '节假日',
                                    title: '请输入节假日名称',
                                    area: ['300px', '50px'] //自定义文本域宽高
                                }, function(value, index, elem){
                                    let company_id =  $("#company").val();
                                    var isLoad;
                                    var field = {};
                                    field.date = last_value;
                                    field.describe = value;
                                    field.company_id = company_id;
                                    $.ajax({
                                        url: '/admin/Holiday/synchrondata',
                                        method: "post",
                                        data: field,
                                        beforeSend: function () {
                                            isLoad = layer.load(0, {
                                                shade: false
                                            }); //0代表加载的风格，支持0-2
                                        },
                                        success: function (result) {
                                            layer.msg(result.msg);
                                            if (result.code === 1) {
                                                setTimeout(function () {
                                                    tableIns.reload();
                                                }, 2000);
                                            }
                                        },
                                        complete: function () {
                                            layer.close(isLoad)
                                        },
                                        error: function (error) {
                                            console.log(error)
                                        }
                                    });
                                    layer.close(index);
                                    return false;
                                });
                            }
                        });
                    }else{
                        let company_id = "{$company_id}";
                        layer.prompt({
                            formType: 2,
                            value: '节假日',
                            title: '请输入节假日名称',
                            area: ['300px', '50px'] //自定义文本域宽高
                        }, function(value, index, elem){
                            var isLoad;
                            var field = {};
                            field.date = last_value;
                            field.describe = value;
                            field.company_id = company_id;
                            $.ajax({
                                url: '/admin/Holiday/synchrondata',
                                method: "post",
                                data: field,
                                beforeSend: function () {
                                    isLoad = layer.load(0, {
                                        shade: false
                                    }); //0代表加载的风格，支持0-2
                                },
                                success: function (result) {
                                    layer.msg(result.msg);
                                    if (result.code === 1) {
                                        setTimeout(function () {
                                            tableIns.reload();
                                        }, 2000);
                                    }
                                },
                                complete: function () {
                                    layer.close(isLoad)
                                },
                                error: function (error) {
                                    console.log(error)
                                }
                            });
                            layer.close(index);
                            return false;
                        });
                    }
                    /*
                    layer.confirm('是否确认设置为节假日?', function (index) {
                        var isLoad;
                        var field = {};
                        field.date = value;
                        $.ajax({
                            url: '/admin/Holiday/synchrondata',
                            method: "post",
                            data: field,
                            beforeSend: function () {
                                isLoad = layer.load(0, {
                                    shade: false
                                }); //0代表加载的风格，支持0-2
                            },
                            success: function (result) {
                                layer.msg(result.msg);
                                if (result.code === 1) {

                                    setTimeout(function () {
                                        location.reload();
                                    }, 1000);


                                }
                            },
                            complete: function () {
                                layer.close(isLoad)
                            },
                            error: function (error) {
                                console.log(error)
                            }
                        });
                        form.render(); //重新渲染
                        return false;

                    });
                    */

                }
                last_value = value;
                if(value == null || value.length <= 0){  //重置
                    last_value = getNowFormatDate();
                }
            }
        });

        //日期
        laydate.render({
            elem: '#date_search',
            type: 'date'
            ,trigger: 'click'
        });

        //数据表格 方法级渲染
        var tableIns = table.render({
            elem: '#LAY_table_user'
            , url: '{:url("Holiday/lists")}'
            , method: 'POST'
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [[
                {field: 'date_name', title: '日期', width: 200, align: 'center'
                },{field: 'date_describe', title: '节假日名称', width: 200, align: 'center',edit: 'text'
                },{field: 'create_time', title: '生成时间', width: 200, align: 'center'
                },  {fixed: 'right', title: '操作', width: 200, align: 'center', toolbar: '#barTable'}
            ]]
            , height: 'full-300'
            , page: true
            , done: function () {
                layer.close(loading);
            }
        });

        table.on('edit(table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            if(field === 'date_describe'){
                $.post('{:url("Holiday/holiday_edit")}'
                    ,{
                        'date_describe':data.date_describe,
                        'id':data.id,
                    }
                    ,function(result){
                        if(result.code === 1){
                            tableIns.reload();
                        }
                    });
            }

        });
        //监听工具条
        table.on('tool(table)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') { //删除
                layer.confirm('真的删除么', function (index) {
                    $.post("{:url('Holiday/date_del')}", {
                        id: data.id
                    }, function (result) {
                        if (result.code === 1) {
                            layer.msg(result.msg, {
                                time: 2000
                            });
                            setTimeout(function () {
                                tableIns.reload();
                            }, 2000)
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    }, "json")
                });

            }

        });
        //搜索项
        $('#search').click(function () {
            var field = {};
            field.date_name = $('input[name=date_name]').val();
            console.log(field);
            tableIns.reload({
                where: field,
                method: 'POST',
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });//清除
        $('#clear').click(function () {
            $('.layui-form-pane').find('.layui-input-inline').each(function (index, element) {
                $(element).find('input[type=text]').val('');
                $(element).find('select').val('');
            })
            tableIns.reload({
                where: {
                    'date_name': '',
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

    });

    //获取当前时间，格式YYYY-MM-DD
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }
</script>